﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    /// <summary>
    /// 单链表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkList<T>
    {
        public LinkListNode<T> Head { set; get; } //单链表头

        //构造
        public LinkList()
        {
            Head = null;
        }

        /// <summary>
        /// 增加新元素到单链表末尾
        /// </summary>
        public void Append(T item)
        {
            LinkListNode<T> foot = new LinkListNode<T>(item);
            LinkListNode<T> A = new LinkListNode<T>();
            if (Head == null)
            {
                Head = foot;
                return;
            }
            A = Head;
            while (A.Next != null)
            {
                A = A.Next;
            }
            A.Next = foot;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="i"></param>
        public void Delete(int i)
        {
            LinkListNode<T> A = new LinkListNode<T>();
            if (i == 1)   //删除头
            {
                A = Head;
                Head = Head.Next;
                return;
            }
            LinkListNode<T> B = new LinkListNode<T>();
            B = Head;
            int j = 1;
            while (B.Next != null && j < i)
            {
                A = B;
                B = B.Next;
                j++;
            }
            if (j == i)
            {
                A.Next = B.Next;
            }

        }
    }
}


